


#library(mvtnorm)
#library(arm)
library(BRugs)
library(R2OpenBUGS)
#library(coda)
#library(car)
library(foreign)

# Change this to your working data directory.  Note this path makes Dropbox work at least on a windows machine
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial")


dataset<-read.dta(file.choose()) # read in a Stata file "tutorial_data.dta"


# globally set constants and variables
n_respondents <- length(dataset$groupid)  # number of units in the analysis
n_sites <- 3  # number of "sites" exogenous variables 
xpostO1<-dataset$xpostO1  # continuous outcome variable
sites<-as.matrix(cbind(dataset$site2, dataset$site3))  # collecting the exogenous varaiables in a matrix object


#  The data and inits objects are used below to create the data and initial value files to send to OpenBUGS
data<-list("n_respondents", "n_sites", "xpostO1", "sites") # to set up the data file, creates a list of the variable and constant names
inits <- function() {  # this is a function that generates random initial values
  list(beta.0=rnorm(1), beta.3=rnorm(n_sites-1), tau.xpostO1=runif(1))} 


# Output the data and inits files to the working directory, using the data and inits objects defined above
setwd("~/../Dropbox/UCR/research/bayes/funglee/persuasion/OBOE/Replication/ResultsInPaper/Tutorial/basic_examples/linear")
bugs.data(data, dir=getwd(), digits=5, data.file="data.txt")  # bugs.data is a function in R2OpenBUGS
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits1.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits2.txt")
bugs.data(inits(), dir=getwd(), digits=3, data.file="inits3.txt")

